Skip to content

Commit

Permalink
feat: update to node-resolve v11
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsDenBakker committed Dec 1, 2020
1 parent 8b9d61f commit b2ccf80
Show file tree
Hide file tree
Showing 9 changed files with 9,771 additions and 22 deletions.
9,726 changes: 9,726 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { Middleware } from 'koa';
import { Options as NodeResolveOptions } from '@rollup/plugin-node-resolve';
import { RollupNodeResolveOptions } from '@rollup/plugin-node-resolve';
import { PolyfillsLoaderConfig } from './utils/inject-polyfills-loader';
import { toBrowserPath, setDebug } from './utils/utils';
import { compatibilityModes } from './constants';
Expand Down Expand Up @@ -80,7 +80,7 @@ export interface Config {
*/
compatibility?: string;
/** whether to resolve bare module imports using node resolve */
nodeResolve?: boolean | NodeResolveOptions;
nodeResolve?: boolean | RollupNodeResolveOptions;
/**
* dedupe ensures only one
* version of a module is ever resolved by resolving it from the root node_modules.
Expand Down Expand Up @@ -157,7 +157,7 @@ export interface ParsedConfig {

// Code transformation
plugins: Plugin[];
nodeResolve: boolean | NodeResolveOptions;
nodeResolve: boolean | RollupNodeResolveOptions;
polyfillsLoader: boolean;
polyfillsLoaderConfig?: Partial<PolyfillsLoaderConfig>;
readUserBabelConfig: boolean;
Expand Down Expand Up @@ -288,11 +288,11 @@ export function createConfig(config: Partial<Config>): ParsedConfig {
customResolveOptions: {
moduleDirectory: moduleDirs,
preserveSymlinks,
},
};
} ,
}as any;

if (dedupeModules) {
nodeResolve.dedupe =
(nodeResolve as any).dedupe =
dedupeModules === true ? importee => !['.', '/'].includes(importee[0]) : dedupeModules;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/babelTransformPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import { Options } from '@rollup/plugin-node-resolve';
import { RollupNodeResolveOptions } from '@rollup/plugin-node-resolve';
import { Plugin } from '../Plugin';
import { createCompatibilityTransform } from '../utils/compatibility-transform';
import { getUserAgentCompat } from '../utils/user-agent-compat';
Expand All @@ -20,7 +20,7 @@ function createFilePath(context: Context, rootDir: string) {
interface BabelTransformConfig {
rootDir: string;
readUserBabelConfig: boolean;
nodeResolve: boolean | Options;
nodeResolve: boolean | RollupNodeResolveOptions;
compatibilityMode: string;
customBabelConfig?: TransformOptions;
fileExtensions: string[];
Expand Down
32 changes: 26 additions & 6 deletions src/plugins/nodeResolvePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import createRollupResolve, { Options } from '@rollup/plugin-node-resolve';
import createRollupResolve, { RollupNodeResolveOptions } from '@rollup/plugin-node-resolve';
import path from 'path';
import fs from 'fs';
import whatwgUrl from 'whatwg-url';
import { Plugin } from '../Plugin';
import { toBrowserPath } from '../utils/utils';

const nodeResolvePackageJson = require('@rollup/plugin-node-resolve/package.json');
function readPkgJson(pkg: string) {
const nodeResolvePath = require.resolve(pkg);
const pkgName = path.join('node_modules', pkg);
const lastPkgDir = nodeResolvePath.lastIndexOf(pkgName);
const pathToPkgDir = nodeResolvePath.substring(0, lastPkgDir);
const pkgDir = path.join(pathToPkgDir, pkgName);
const pkgJsonPath = path.join(pkgDir, 'package.json');
if (!fs.existsSync(pkgJsonPath)) {
return undefined;
}

const pkgJsonString = fs.readFileSync(pkgJsonPath, 'utf-8');
return JSON.parse(pkgJsonString);
}

const nodeResolvePackageJson = readPkgJson('@rollup/plugin-node-resolve');

const fakePluginContext = {
meta: {
Expand All @@ -18,29 +34,33 @@ const fakePluginContext = {
interface NodeResolveConfig {
rootDir: string;
fileExtensions: string[];
nodeResolve: boolean | Options;
nodeResolve: boolean | RollupNodeResolveOptions;
}

export function nodeResolvePlugin(config: NodeResolveConfig): Plugin {
const { fileExtensions, rootDir } = config;
const userOptions = typeof config.nodeResolve === 'object' ? config.nodeResolve : {};
const customResolveOptions: any = (userOptions as any)?.customResolveOptions ?? {};
const options = {
rootDir,
// allow resolving polyfills for nodejs libs
preferBuiltins: false,
extensions: fileExtensions,
...(typeof config.nodeResolve === 'object' ? config.nodeResolve : {}),
...userOptions,
customResolveOptions: undefined,
moduleDirectories: customResolveOptions?.moduleDirectory,
};
const preserveSymlinks = !!customResolveOptions?.preserveSymlinks;
const nodeResolve = createRollupResolve(options);

// call buildStart
const preserveSymlinks = options?.customResolveOptions?.preserveSymlinks;
nodeResolve.buildStart?.call(fakePluginContext as any, { preserveSymlinks });

return {
async serverStart({ config }) {},

async resolveImport({ source, context }) {
if (! path.isAbsolute(source) && whatwgUrl.parseURL(source) != null) {
if (!path.isAbsolute(source) && whatwgUrl.parseURL(source) != null) {
// don't resolve relative and valid urls
return source;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/compatibility-transform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransformOptions } from '@babel/core';
import { Options } from '@rollup/plugin-node-resolve';
import { RollupNodeResolveOptions } from '@rollup/plugin-node-resolve';
import { BabelTransform } from './babel-transform';
import { UserAgentCompat } from './user-agent-compat';
import minimatch from 'minimatch';
Expand All @@ -16,7 +16,7 @@ import { logDebug } from './utils';
export interface CompatibilityTransformConfig {
rootDir: string;
readUserBabelConfig: boolean;
nodeResolve: boolean | Options;
nodeResolve: boolean | RollupNodeResolveOptions;
compatibilityMode: string;
customBabelConfig?: TransformOptions;
fileExtensions: string[];
Expand Down
2 changes: 2 additions & 0 deletions test/plugins/nodeResolvePlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ async function testNodeResolve({

expect(response.status).to.equal(200);
expect(responseText).to.equal(expected);
} catch (error) {
console.error(error);
} finally {
server.close();
}
Expand Down
6 changes: 3 additions & 3 deletions test/snapshots/polyfills-loader/inline-scripts-exclude.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@
}

if (!('fetch' in window) || !('Request' in window) || !('signal' in window.Request.prototype)) {
polyfills.push(loadScript('./polyfills/fetch.32366c3902f1d160696086309543c0db.js'));
polyfills.push(loadScript('./polyfills/fetch.3a25c03b21a48c3f864a76352c53625e.js'));
}

if (!('attachShadow' in Element.prototype) || !('getRootNode' in Element.prototype) || window.ShadyDOM && window.ShadyDOM.force) {
polyfills.push(loadScript('./polyfills/webcomponents.73f0ff428117951339e4deabad8949ed.js'));
polyfills.push(loadScript('./polyfills/webcomponents.ed846db3e7c3b1354dcb129b8858e4d4.js'));
}

if (!('noModule' in HTMLScriptElement.prototype) && 'getRootNode' in Element.prototype) {
Expand All @@ -107,7 +107,7 @@
polyfillsLoader();
}

s.src = "polyfills/core-js.a8ea21cd3f54f879f981d1dccc3c2a0c.js";
s.src = "polyfills/core-js.ef187033b1f6d4e436278a385a4811e1.js";
s.onload = onLoaded;

s.onerror = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
}

if (!('fetch' in window) || !('Request' in window) || !('signal' in window.Request.prototype)) {
polyfills.push(loadScript('./polyfills/fetch.32366c3902f1d160696086309543c0db.js'));
polyfills.push(loadScript('./polyfills/fetch.3a25c03b21a48c3f864a76352c53625e.js'));
}

if (!('attachShadow' in Element.prototype) || !('getRootNode' in Element.prototype) || window.ShadyDOM && window.ShadyDOM.force) {
polyfills.push(loadScript('./polyfills/webcomponents.73f0ff428117951339e4deabad8949ed.js'));
polyfills.push(loadScript('./polyfills/webcomponents.ed846db3e7c3b1354dcb129b8858e4d4.js'));
}

if (!('noModule' in HTMLScriptElement.prototype) && 'getRootNode' in Element.prototype) {
Expand Down Expand Up @@ -84,7 +84,7 @@
polyfillsLoader();
}

s.src = "polyfills/core-js.a8ea21cd3f54f879f981d1dccc3c2a0c.js";
s.src = "polyfills/core-js.ef187033b1f6d4e436278a385a4811e1.js";
s.onload = onLoaded;

s.onerror = function () {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"declaration": true,
"types": ["node", "mocha"],
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true
}
}

0 comments on commit b2ccf80

Please sign in to comment.