Skip to content

Commit

Permalink
Merge pull request #19 from prateekbh/deps-fix
Browse files Browse the repository at this point in the history
Deps fix
  • Loading branch information
prateekbh committed Jun 7, 2019
2 parents e0bd23c + 91b934c commit 5d2c99d
Show file tree
Hide file tree
Showing 8 changed files with 6,343 additions and 7,444 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "all",
"parser": "typescript",
"singleQuote": true
}
7,394 changes: 0 additions & 7,394 deletions package-lock.json

This file was deleted.

25 changes: 21 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,37 @@
},
"homepage": "https://github.com/prateekbh/babel-esm-plugin#readme",
"dependencies": {
"ava": "^1.0.1",
"deepcopy": "^1.0.0",
"webpack": "^4.28.4"
"chalk": "^2.4.1",
"deepcopy": "^1.0.0"
},
"peerDependencies": {
"webpack": "^4.28.4"
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"ava": "^1.0.1",
"babel-loader": "^8.0.5",
"chalk": "^2.4.1",
"esm": "^3.0.84",
"husky": "^2.4.0",
"lint-staged": "^8.2.0",
"prettier": "^1.18.0",
"release-it": "^12.0.1",
"webpack": "^4.28.4",
"webpack-cli": "^3.2.1"
},
"lint-staged": {
"linters": {
"{src,tests}/**/*.js": [
"prettier --write",
"git add"
]
},
"ignore": ["tests/**/fixtures/*.js"]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
120 changes: 77 additions & 43 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,32 @@ const CHUNK_FILENAME = '[id].es6.js';

class BabelEsmPlugin {
constructor(options) {
this.options_ = Object.assign({
filename: FILENAME,
chunkFilename: CHUNK_FILENAME,
excludedPlugins: [PLUGIN_NAME],
additionalPlugins: []
}, options);
this.options_ = Object.assign(
{
filename: FILENAME,
chunkFilename: CHUNK_FILENAME,
excludedPlugins: [PLUGIN_NAME],
additionalPlugins: [],
},
options,
);
}

apply(compiler) {
compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
const outputOptions = deepcopy(compiler.options);
this.babelLoaderConfigOptions_ = this.getBabelLoaderOptions(outputOptions);
this.newConfigOptions_ = this.makeESMPresetOptions(this.babelLoaderConfigOptions_);
this.babelLoaderConfigOptions_ = this.getBabelLoaderOptions(
outputOptions,
);
this.newConfigOptions_ = this.makeESMPresetOptions(
this.babelLoaderConfigOptions_,
);
outputOptions.output.filename = this.options_.filename;
outputOptions.output.chunkFilename = this.options_.chunkFilename;
// Only copy over mini-extract-text-plugin (excluding it breaks extraction entirely)
let plugins = (compiler.options.plugins || []).filter(c => this.options_.excludedPlugins.indexOf(c.constructor.name) < 0);
let plugins = (compiler.options.plugins || []).filter(
c => this.options_.excludedPlugins.indexOf(c.constructor.name) < 0,
);

// Add the additionalPlugins
plugins = plugins.concat(this.options_.additionalPlugins);
Expand All @@ -41,7 +50,10 @@ class BabelEsmPlugin {
* which a few plugins might expect while execution the apply method.
* We do call the `apply` method of all plugins by ourselves later in the code
*/
const childCompiler = compilation.createChildCompiler(PLUGIN_NAME, outputOptions.output);
const childCompiler = compilation.createChildCompiler(
PLUGIN_NAME,
outputOptions.output,
);

childCompiler.context = compiler.context;
childCompiler.inputFileSystem = compiler.inputFileSystem;
Expand All @@ -55,11 +67,15 @@ class BabelEsmPlugin {
}

Object.keys(compiler.options.entry).forEach(entry => {
const entryFiles = compiler.options.entry[entry]
const entryFiles = compiler.options.entry[entry];
if (Array.isArray(entryFiles)) {
new MultiEntryPlugin(compiler.context, entryFiles, entry).apply(childCompiler);
new MultiEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
);
} else {
new SingleEntryPlugin(compiler.context, entryFiles, entry).apply(childCompiler);
new SingleEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
);
}
});

Expand All @@ -69,37 +85,42 @@ class BabelEsmPlugin {
if (compiler.options.optimization) {
if (compiler.options.optimization.splitChunks) {
new SplitChunksPlugin(
Object.assign(
{},
compiler.options.optimization.splitChunks,
)
Object.assign({}, compiler.options.optimization.splitChunks),
).apply(childCompiler);
}
}

compilation.hooks.additionalAssets.tapAsync(PLUGIN_NAME, (childProcessDone) => {
let babelLoader;
childCompiler.options.module.rules.forEach((rule, index) => {
babelLoader = this.getBabelLoader(childCompiler.options);
babelLoader.options = this.newConfigOptions_;
});

this.options_.beforeStartExecution && this.options_.beforeStartExecution(plugins, (babelLoader || {}).options);
compilation.hooks.additionalAssets.tapAsync(
PLUGIN_NAME,
childProcessDone => {
let babelLoader;
childCompiler.options.module.rules.forEach((rule, index) => {
babelLoader = this.getBabelLoader(childCompiler.options);
babelLoader.options = this.newConfigOptions_;
});

childCompiler.runAsChild((err, entries, childCompilation) => {
if (!err) {
compilation.assets = Object.assign(childCompilation.assets,
compilation.assets
this.options_.beforeStartExecution &&
this.options_.beforeStartExecution(
plugins,
(babelLoader || {}).options,
);
compilation.namedChunkGroups = Object.assign(
childCompilation.namedChunkGroups,
compilation.namedChunkGroups
);
}
err && compilation.errors.push(err);
childProcessDone();
});
});

childCompiler.runAsChild((err, entries, childCompilation) => {
if (!err) {
compilation.assets = Object.assign(
childCompilation.assets,
compilation.assets,
);
compilation.namedChunkGroups = Object.assign(
childCompilation.namedChunkGroups,
compilation.namedChunkGroups,
);
}
err && compilation.errors.push(err);
childProcessDone();
});
},
);
callback();
});
}
Expand All @@ -118,7 +139,12 @@ class BabelEsmPlugin {
babelConfig = rule;
}
});
} else if ((rule.use && rule.use.loader && rule.use.loader.includes(BABEL_LOADER_NAME)) || rule.loader.includes(BABEL_LOADER_NAME)) {
} else if (
(rule.use &&
rule.use.loader &&
rule.use.loader.includes(BABEL_LOADER_NAME)) ||
rule.loader.includes(BABEL_LOADER_NAME)
) {
babelConfig = rule.use || rule;
}
}
Expand Down Expand Up @@ -149,15 +175,23 @@ class BabelEsmPlugin {
options.presets.forEach(preset => {
if (!Array.isArray(preset)) return;
const [name, options] = preset;
if (name.includes('@babel/preset-env') || name.includes('@babel\\preset-env')) {
if (
name.includes('@babel/preset-env') ||
name.includes('@babel\\preset-env')
) {
found = true;
options.targets = options.targets || {};
options.targets = { "esmodules": true };
options.targets = { esmodules: true };
}
});
if (!found) {
console.log(chalk.yellow('Adding @babel/preset-env because it was not found'));
options.presets.push(['@babel/preset-env', { targets: { "esmodules": true } }])
console.log(
chalk.yellow('Adding @babel/preset-env because it was not found'),
);
options.presets.push([
'@babel/preset-env',
{ targets: { esmodules: true } },
]);
}
return options;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/multiple-entry-files/fixtures/output.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ let config = {
a: 10,
b: 20
};
const a = config.a;
const {
a
} = config;

/***/ }),
/* 2 */
Expand Down
4 changes: 3 additions & 1 deletion tests/require-resolve/fixtures/output.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ let config = {
a: 10,
b: 20
};
const a = config.a;
const {
a
} = config;
const init = [1, 2, 3, 4];
const final = [...init, 5];

Expand Down
4 changes: 3 additions & 1 deletion tests/single-loader/fixtures/output.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ let config = {
a: 10,
b: 20
};
const a = config.a;
const {
a
} = config;
const init = [1, 2, 3, 4];
const final = [...init, 5];

Expand Down

0 comments on commit 5d2c99d

Please sign in to comment.