Skip to content

Commit

Permalink
Merge pull request #33 from peerigon/fix/webpack4-compat
Browse files Browse the repository at this point in the history
deps: Add webpack 4 compatibility
  • Loading branch information
jhnns authored Mar 19, 2018
2 parents a80e3be + 0a6a715 commit 77f1a67
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
25 changes: 17 additions & 8 deletions examples/index-html/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var path = require("path");
var indexHtml = path.join(__dirname, "app", "index.html");

module.exports = {
mode: "development",
entry: [
path.join(__dirname, "app", "main.js"),
indexHtml
Expand All @@ -12,28 +13,36 @@ module.exports = {
filename: "bundle.js"
},
module: {
loaders: [
rules: [
{
test: indexHtml,
loaders: [
"file-loader?name=[name].[ext]",
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]"
}
},
path.resolve(__dirname, "..", "..", "lib", "extractLoader.js"),
"html-loader?" + JSON.stringify({
attrs: ["img:src", "link:href"]
})
{
loader: "html-loader",
options: {
attrs: ["img:src", "link:href"]
}
}
]
},
{
test: /\.css$/,
loaders: [
use: [
"file-loader",
path.resolve(__dirname, "..", "..", "lib", "extractLoader.js"),
"css-loader"
]
},
{
test: /\.jpg$/,
loader: "file-loader"
use: "file-loader"
}
]
}
Expand Down
1 change: 1 addition & 0 deletions examples/main-css/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if (live) {
}

module.exports = {
mode: live ? "production" : "development",
entry: [
path.join(__dirname, "app", "main.js"),
mainCss.join("!")
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"nyc": "^11.4.1",
"rimraf": "^2.6.2",
"style-loader": "^0.19.1",
"webpack": "^3.10.0"
"webpack": "^4.0.0",
"webpack-cli": "^2.0.0"
},
"dependencies": {
"loader-utils": "^1.1.0"
Expand Down
30 changes: 29 additions & 1 deletion src/extractLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const rndPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + rndNumber() + rndNumbe
function extractLoader(content) {
const callback = this.async();
const options = getOptions(this) || {};
const publicPath = options.publicPath === undefined ? this.options.output.publicPath : options.publicPath;
const publicPath = getPublicPath(options, this);
const dependencies = [];
const script = new vm.Script(content, {
filename: this.resourcePath,
Expand Down Expand Up @@ -123,6 +123,34 @@ function rndNumber() {
.slice(2);
}

/**
* Retrieves the public path from the loader options, context.options (webpack <4) or context._compilation (webpack 4+).
* context._compilation is likely to get removed in a future release, so this whole function should be removed then.
* See: https://github.com/peerigon/extract-loader/issues/35
*
* @deprecated
* @param {Object} options - Extract-loader options
* @param {Object} context - Webpack loader context
* @returns {string}
*/
function getPublicPath(options, context) {
const property = "publicPath";

if (property in options) {
return options[property];
}

if (context.options && context.options.output && property in context.options.output) {
return context.options.output[property];
}

if (context._compilation && context._compilation.outputOptions && property in context._compilation.outputOptions) {
return context._compilation.outputOptions[property];
}

return "";
}

// For CommonJS interoperability
module.exports = extractLoader;
export default extractLoader;
5 changes: 2 additions & 3 deletions test/extractLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ describe("extractLoader", () => {
it("should track all dependencies", () =>
compile({ testModule: "stylesheet.html" }).then(stats => {
const basePath = path.dirname(__dirname); // returns the parent dirname
const dependencies = stats.compilation.fileDependencies.map(
dependency => dependency.slice(basePath.length)
);
const dependencies = Array.from(stats.compilation.fileDependencies)
.map(dependency => dependency.slice(basePath.length));

expect(dependencies.sort()).to.eql(
[
Expand Down
3 changes: 2 additions & 1 deletion test/support/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function ({ testModule, publicPath, loaderOptions }) {
return new Promise((resolve, reject) => {
webpack(
{
mode: "development",
entry: testModulePath,
output: {
path: path.resolve(__dirname, "../dist"),
Expand Down Expand Up @@ -92,7 +93,7 @@ export default function ({ testModule, publicPath, loaderOptions }) {
},
(err, stats) => {
if (err || stats.hasErrors() || stats.hasWarnings()) {
reject(err || stats.toString("errors-only"));
reject(err || stats.toString("minimal"));
} else {
resolve(stats);
}
Expand Down

0 comments on commit 77f1a67

Please sign in to comment.