├── .babelrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── spec ├── example-wcs │ ├── generic-wc │ │ ├── index.css │ │ ├── index.html │ │ └── index.js │ ├── linked-css-files │ │ ├── foo │ │ │ └── index.css │ │ ├── index.css │ │ └── index.html │ ├── linked-js-files │ │ ├── foo │ │ │ └── index.js │ │ ├── index.html │ │ └── index.js │ ├── nested-html-imports │ │ ├── index.html │ │ └── other-wc │ │ │ ├── index.html │ │ │ └── index.js │ └── simple │ │ └── index.html ├── index.spec.js └── support │ └── jasmine.json └── src └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015" 4 | ] 5 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | dist 3 | lib 4 | node_modules 5 | spec/example-wcs/output 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 5.0.0 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Ray Nicholus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Webpack web-components-loader 2 | 3 | [![Build Status](https://travis-ci.org/rnicholus/web-components-loader.svg?branch=master)](https://travis-ci.org/rnicholus/web-components-loader) 4 | [![npm](https://img.shields.io/npm/v/web-components-loader.svg)](https://www.npmjs.com/package/web-components-loader) 5 | [![license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE) 6 | 7 | > A Webpack loader that makes it incredibly easy to import HTML-centric Web Components into your project. 8 | 9 | Importing a Web Component that consists of a single JavaScript file isn't particularly difficult, but what about Web Components that require HTML imports which themselves import various other JavaScript, CSS, and other HTML files? This is more more complicated, and, before web-components-loader, involved a lot of manual intervention. 10 | 11 | Simply `import` or `require` the HTML file for the web component you'd like to pull into your project. The loader does the following for you: 12 | 13 | 1. Copies the HTML file and all of its dependencies to a namespaced location in your public/output directory. It determines the Web Component's dependencies by parsing the tree of files, starting with the root HTML file. 14 | 2. Optionally minifies all related HTML, CSS, and JavaScript files. 15 | 3. Makes it simple to transpile the JavaScript files associated with your Web Component, or augment them in any other way you choose, using an intutive callback mechanism. 16 | 4. Watches all files associated with your Web Component and triggers a rebuild whenever any of them change (assuming the Webpack watcher is running). 17 | 5. Returns the location of the public path to the root HTML file. You can then add this to an HTML import element in your project. 18 | 19 | ## Installing 20 | 21 | This will install the loader and update your `package.json` file with the appropriate entry/version: 22 | 23 | ```bash 24 | npm install web-components-loader --save-dev 25 | ``` 26 | 27 | ## Configuration 28 | 29 | All configuration lives inside of your Webpack configuration file. 30 | 31 | ### Query parameters 32 | 33 | All query parameters are sub-properties of the loader's `query` property: 34 | 35 | - `minify` - `true` to minify all HTML, JS, and CSS files for imported Web Components 36 | 37 | - `outputPath` - This takes presedence over the `ouput.path` property specified elsewhere in your Webpack config. Specify this param to override the output path used by the loader when writing out your Web Component files. 38 | 39 | - `outputPublicPath` - This takes presedence over the `ouput.publicPath` property specified elsewhere in your Webpack config. Specify this param to override the output public path used by the loader when generating the public path returned by the loader. 40 | 41 | ### Options 42 | 43 | If you'd like to augment/transpile the JavaScript files associated with an imported Web Compoenent, you can do so by defining a "callback" function as a sub-property of a root `webComponentsLoader` object in your Webpack configuration: 44 | 45 | ```js 46 | webComponentsLoader: { 47 | transformJs: jsFileContents => { 48 | //...do something to the JS file contents string 49 | const newContents = transpile(jsFileContents) 50 | 51 | return newContents 52 | } 53 | } 54 | ``` 55 | 56 | ## Simplest possible configuration 57 | 58 | This will key on any imported/required HTML files inside a `web-components` directory: 59 | 60 | ```js 61 | // webpack.config.js 62 | 63 | module.exports = { 64 | entry: { 65 | main: '/src/main.js' 66 | }, 67 | output: { 68 | path: '/public', 69 | publicPath: '/public', 70 | filename: '[name].bundle.js' 71 | }, 72 | module: { 73 | loaders: [ 74 | { 75 | test: /web-components\//, 76 | loader: 'web-components-loader' 77 | } 78 | ] 79 | } 80 | } 81 | ``` 82 | 83 | For example: 84 | 85 | ```js 86 | // main.js 87 | 88 | var htmlHrefPath = require('/src/web-components/my-component.html') 89 | 90 | var importEl = document.createElement('link') 91 | importEl.rel = 'import' 92 | importEl.href = htmlHrefPath 93 | 94 | document.body.appendChild(importEl) 95 | ``` 96 | 97 | ## Minifying all imported Web Component HTML, CSS, and JS files 98 | 99 | ```js 100 | { 101 | test: /web-components\//, 102 | loader: 'web-components-loader', 103 | query: { 104 | minify: true 105 | } 106 | } 107 | ``` 108 | 109 | ## Transpiling Web Component JS Files 110 | 111 | ```js 112 | // webpack.config.js 113 | 114 | var babel = require('babel-core') 115 | 116 | module.exports = { 117 | entry: { 118 | main: '/src/main.js' 119 | }, 120 | output: { 121 | path: '/public', 122 | publicPath: '/public', 123 | filename: '[name].bundle.js' 124 | }, 125 | webComponentsLoader: { 126 | transformJs: rawCode => { 127 | return babel.transform(rawCode, { 128 | presets: ['es2015'] 129 | }).code; 130 | }, 131 | }, 132 | module: { 133 | loaders: [ 134 | { 135 | test: /web-components\//, 136 | loader: 'web-components-loader' 137 | } 138 | ] 139 | } 140 | } 141 | ``` 142 | 143 | ## Using Web Components with React 144 | 145 | This loader works very well with the [React Web Component Wrapper](https://github.com/rnicholus/react-web-component-wrapper) component, which allows you to use Web Components using familiar React conventions. See the wrapper project for details, but integration may look something like this (provided you have already configured Webpack as described above): 146 | 147 | ```jsx 148 | import React, { Component } from 'react' 149 | import webComponentHref from 'file-input-web-component/file-input.html' 150 | import FileInput from 'web-component-wrapper' 151 | 152 | const extensions = [ 153 | 'jpg', 154 | 'jpeg' 155 | ] 156 | 157 | class FileInputDemo extends Component { 158 | render() { 159 | return ( 160 | console.log(event.detail) } 162 | webComponentHtmlHref={ webComponentHref } 163 | webComponentName='file-input' 164 | > 165 | Select Files 166 | 167 | ) 168 | } 169 | } 170 | 171 | export default FileInputDemo 172 | ``` 173 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web-components-loader", 3 | "version": "0.1.2", 4 | "description": "Easily load web components into a project using Webpack.", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "build": "mkdir -p lib; babel src/index.js -o lib/index.js", 8 | "release": "rm -rf dist; mkdir dist; cp -pR src lib package.json LICENSE README.md dist/; (cd dist; npm publish)", 9 | "test": "jasmine" 10 | }, 11 | "homepage": "https://github.com/rnicholus/web-components-loader#readme", 12 | "repository": "https://github.com/rnicholus/web-components-loader", 13 | "keywords": [ 14 | "webpack", 15 | "javascript", 16 | "webcomponents", 17 | "customelements", 18 | "htmlimport", 19 | "react", 20 | "vue", 21 | "angular" 22 | ], 23 | "author": { 24 | "name": "Ray Nicholus", 25 | "url": "http://raynicholus.com" 26 | }, 27 | "license": "MIT", 28 | "dependencies": { 29 | "html-minifier": "3.2.3", 30 | "loader-utils": "0.2.16", 31 | "mkdirp": "0.5.1", 32 | "uglify-js": "2.7.4", 33 | "uglifycss": "0.0.25", 34 | "xmldom": "0.1.24" 35 | }, 36 | "devDependencies": { 37 | "babel-cli": "6.18.0", 38 | "babel-core": "6.18.2", 39 | "babel-preset-es2015": "6.18.0", 40 | "del": "2.2.2", 41 | "jasmine": "2.5.2" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /spec/example-wcs/generic-wc/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | display: none; 3 | } -------------------------------------------------------------------------------- /spec/example-wcs/generic-wc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | -------------------------------------------------------------------------------- /spec/example-wcs/generic-wc/index.js: -------------------------------------------------------------------------------- 1 | var hello = 'hi'; 2 | var foo = 'bar'; 3 | console.log(hello, foo); -------------------------------------------------------------------------------- /spec/example-wcs/linked-css-files/foo/index.css: -------------------------------------------------------------------------------- 1 | body {} -------------------------------------------------------------------------------- /spec/example-wcs/linked-css-files/index.css: -------------------------------------------------------------------------------- 1 | body {} -------------------------------------------------------------------------------- /spec/example-wcs/linked-css-files/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | -------------------------------------------------------------------------------- /spec/example-wcs/linked-js-files/foo/index.js: -------------------------------------------------------------------------------- 1 | console.log('foo') -------------------------------------------------------------------------------- /spec/example-wcs/linked-js-files/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | -------------------------------------------------------------------------------- /spec/example-wcs/linked-js-files/index.js: -------------------------------------------------------------------------------- 1 | console.log('root') -------------------------------------------------------------------------------- /spec/example-wcs/nested-html-imports/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | -------------------------------------------------------------------------------- /spec/example-wcs/nested-html-imports/other-wc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | -------------------------------------------------------------------------------- /spec/example-wcs/nested-html-imports/other-wc/index.js: -------------------------------------------------------------------------------- 1 | console.log('other-wc') -------------------------------------------------------------------------------- /spec/example-wcs/simple/index.html: -------------------------------------------------------------------------------- 1 | 4 | 5 | -------------------------------------------------------------------------------- /spec/index.spec.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const loader = require(path.resolve('./src')) 3 | 4 | const del = require('del') 5 | const fs = require('fs') 6 | 7 | const outputDir = 'spec/example-wcs/output' 8 | 9 | describe('web-components-loader', () => { 10 | beforeEach(() => { 11 | del.sync([`${outputDir}/**`]) 12 | fs.mkdirSync(outputDir) 13 | }) 14 | 15 | describe('simple WC support', () => { 16 | const htmlPath = 'spec/example-wcs/simple/index.html' 17 | const htmlContent = fs.readFileSync(htmlPath).toString() 18 | const context = { 19 | addDependency: jasmine.createSpy('addDependency'), 20 | cacheable: () => {}, 21 | context: 'spec/example-wcs/simple', 22 | options: { 23 | output: { 24 | path: outputDir, 25 | publicPath: '' 26 | } 27 | }, 28 | resourcePath: htmlPath 29 | } 30 | 31 | it('copies the HTML file to the output dir and returns output path', () => { 32 | const outputPath = loader.call(context, htmlContent) 33 | 34 | expect(outputPath).toBe("module.exports = 'web-components/simple/index.html'") 35 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/simple/index.html') 36 | expect(fs.existsSync(`${outputDir}/web-components/simple/index.html`)).toBe(true) 37 | expect(fs.existsSync(`${outputDir}/web-components/simple/index.html`)).toBe(true) 38 | }) 39 | }) 40 | 41 | describe('WC with linked JS files', () => { 42 | const htmlPath = 'spec/example-wcs/linked-js-files/index.html' 43 | const htmlContent = fs.readFileSync(htmlPath).toString() 44 | const context = { 45 | addDependency: jasmine.createSpy('addDependency'), 46 | cacheable: () => {}, 47 | context: 'spec/example-wcs/linked-js-files', 48 | options: { 49 | output: { 50 | path: outputDir, 51 | publicPath: '' 52 | } 53 | }, 54 | resourcePath: htmlPath 55 | } 56 | 57 | it('copies the HTML & JS files to the output dir and returns output path to HTML file', () => { 58 | const outputPath = loader.call(context, htmlContent) 59 | 60 | expect(outputPath).toBe("module.exports = 'web-components/linked-js-files/index.html'") 61 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/linked-js-files/index.html') 62 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/linked-js-files/index.js') 63 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/linked-js-files/foo/index.js') 64 | expect(fs.existsSync(`${outputDir}/web-components/linked-js-files/index.html`)).toBe(true) 65 | expect(fs.existsSync(`${outputDir}/web-components/linked-js-files/index.js`)).toBe(true) 66 | expect(fs.existsSync(`${outputDir}/web-components/linked-js-files/foo/index.js`)).toBe(true) 67 | }) 68 | }) 69 | 70 | describe('WC with linked CSS files', () => { 71 | const htmlPath = 'spec/example-wcs/linked-css-files/index.html' 72 | const htmlContent = fs.readFileSync(htmlPath).toString() 73 | const context = { 74 | addDependency: jasmine.createSpy('addDependency'), 75 | cacheable: () => {}, 76 | context: 'spec/example-wcs/linked-css-files', 77 | options: { 78 | output: { 79 | path: outputDir, 80 | publicPath: '' 81 | } 82 | }, 83 | resourcePath: htmlPath 84 | } 85 | 86 | it('copies the HTML & CSS files to the output dir and returns output path to HTML file', () => { 87 | const outputPath = loader.call(context, htmlContent) 88 | 89 | expect(outputPath).toBe("module.exports = 'web-components/linked-css-files/index.html'") 90 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/linked-css-files/index.html') 91 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/linked-css-files/index.css') 92 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/linked-css-files/foo/index.css') 93 | expect(fs.existsSync(`${outputDir}/web-components/linked-css-files/index.html`)).toBe(true) 94 | expect(fs.existsSync(`${outputDir}/web-components/linked-css-files/index.css`)).toBe(true) 95 | expect(fs.existsSync(`${outputDir}/web-components/linked-css-files/foo/index.css`)).toBe(true) 96 | }) 97 | }) 98 | 99 | describe('WC with nested HTML imports', () => { 100 | const htmlPath = 'spec/example-wcs/nested-html-imports/index.html' 101 | const htmlContent = fs.readFileSync(htmlPath).toString() 102 | const context = { 103 | addDependency: jasmine.createSpy('addDependency'), 104 | cacheable: () => {}, 105 | context: 'spec/example-wcs/nested-html-imports', 106 | options: { 107 | output: { 108 | path: outputDir, 109 | publicPath: '' 110 | } 111 | }, 112 | resourcePath: htmlPath 113 | } 114 | 115 | it('copies the HTML, JS, and nested HTML files to the output dir and returns output path to root HTML file', () => { 116 | const outputPath = loader.call(context, htmlContent) 117 | 118 | expect(outputPath).toBe("module.exports = 'web-components/nested-html-imports/index.html'") 119 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/nested-html-imports/index.html') 120 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/nested-html-imports/other-wc/index.html') 121 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/nested-html-imports/other-wc/index.html') 122 | expect(fs.existsSync(`${outputDir}/web-components/nested-html-imports/index.html`)).toBe(true) 123 | expect(fs.existsSync(`${outputDir}/web-components/nested-html-imports/other-wc/index.html`)).toBe(true) 124 | expect(fs.existsSync(`${outputDir}/web-components/nested-html-imports/other-wc/index.js`)).toBe(true) 125 | }) 126 | }) 127 | 128 | describe('minification', () => { 129 | const htmlPath = 'spec/example-wcs/generic-wc/index.html' 130 | const htmlContent = fs.readFileSync(htmlPath).toString() 131 | const defaultContext = { 132 | addDependency: () => {}, 133 | cacheable: () => {}, 134 | context: 'spec/example-wcs/generic-wc', 135 | options: { 136 | output: { 137 | path: outputDir, 138 | publicPath: '' 139 | } 140 | }, 141 | resourcePath: htmlPath 142 | } 143 | 144 | it('does not minify JS, CSS, or HTML files by default', () => { 145 | loader.call(defaultContext, htmlContent) 146 | 147 | expect(fs.statSync(htmlPath).size) 148 | .toBe(fs.statSync(`${outputDir}/web-components/generic-wc/index.html`).size) 149 | 150 | expect(fs.statSync(`${defaultContext.context}/index.js`).size) 151 | .toBe(fs.statSync(`${outputDir}/web-components/generic-wc/index.js`).size) 152 | 153 | expect(fs.statSync(`${defaultContext.context}/index.css`).size) 154 | .toBe(fs.statSync(`${outputDir}/web-components/generic-wc/index.css`).size) 155 | }) 156 | 157 | it('minifies JS, CSS, or HTML files if ordered to do so', () => { 158 | const contextWithMinifyEnabled = 159 | Object.assign({}, defaultContext, { query: '?minify' }) 160 | 161 | loader.call(contextWithMinifyEnabled, htmlContent) 162 | 163 | expect(fs.statSync(htmlPath).size) 164 | .toBeGreaterThan(fs.statSync(`${outputDir}/web-components/generic-wc/index.html`).size) 165 | 166 | expect(fs.statSync(`${contextWithMinifyEnabled.context}/index.js`).size) 167 | .toBeGreaterThan(fs.statSync(`${outputDir}/web-components/generic-wc/index.js`).size) 168 | 169 | expect(fs.statSync(`${contextWithMinifyEnabled.context}/index.css`).size) 170 | .toBeGreaterThan(fs.statSync(`${outputDir}/web-components/generic-wc/index.css`).size) 171 | }) 172 | }) 173 | 174 | describe('transform', () => { 175 | const htmlPath = 'spec/example-wcs/generic-wc/index.html' 176 | const htmlContent = fs.readFileSync(htmlPath).toString() 177 | const context = { 178 | addDependency: () => {}, 179 | cacheable: () => {}, 180 | context: 'spec/example-wcs/generic-wc', 181 | options: { 182 | output: { 183 | path: outputDir, 184 | publicPath: '' 185 | }, 186 | webComponentsLoader: { 187 | transformJs: code => { 188 | return `start_${code}_end` 189 | } 190 | } 191 | }, 192 | query: `?output=${outputDir}`, 193 | resourcePath: htmlPath 194 | } 195 | 196 | it('outputs transformed JS', () => { 197 | const rawJsFileContent = fs.readFileSync(`${context.context}/index.js`).toString() 198 | 199 | loader.call(context, htmlContent) 200 | 201 | expect(fs.readFileSync(`${outputDir}/web-components/generic-wc/index.js`).toString()) 202 | .toBe(`start_${rawJsFileContent}_end`) 203 | }) 204 | }) 205 | 206 | describe('override path and/or publicPath', () => { 207 | const htmlPath = 'spec/example-wcs/nested-html-imports/index.html' 208 | const htmlContent = fs.readFileSync(htmlPath).toString() 209 | const context = { 210 | addDependency: jasmine.createSpy('addDependency'), 211 | cacheable: () => {}, 212 | context: 'spec/example-wcs/nested-html-imports', 213 | options: { 214 | output: { 215 | path: 'this/is/wrong', 216 | publicPath: 'this/is/very/wrong' 217 | } 218 | }, 219 | resourcePath: htmlPath 220 | } 221 | 222 | it('handles overriden path and publicPath', () => { 223 | const contextWithOverridenPath = Object.assign({}, context, { 224 | query: `?outputPath=${outputDir}&outputPublicPath=` 225 | }) 226 | const outputPath = loader.call(contextWithOverridenPath, htmlContent) 227 | 228 | expect(outputPath).toBe("module.exports = 'web-components/nested-html-imports/index.html'") 229 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/nested-html-imports/index.html') 230 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/nested-html-imports/other-wc/index.html') 231 | expect(context.addDependency).toHaveBeenCalledWith('spec/example-wcs/nested-html-imports/other-wc/index.html') 232 | expect(fs.existsSync(`${outputDir}/web-components/nested-html-imports/index.html`)).toBe(true) 233 | expect(fs.existsSync(`${outputDir}/web-components/nested-html-imports/other-wc/index.html`)).toBe(true) 234 | expect(fs.existsSync(`${outputDir}/web-components/nested-html-imports/other-wc/index.js`)).toBe(true) 235 | }) 236 | }) 237 | }) -------------------------------------------------------------------------------- /spec/support/jasmine.json: -------------------------------------------------------------------------------- 1 | { 2 | "spec_dir": "spec", 3 | "spec_files": [ 4 | "**/*[sS]pec.js" 5 | ], 6 | "helpers": [ 7 | "helpers/**/*.js" 8 | ], 9 | "stopSpecOnExpectationFailure": false, 10 | "random": false 11 | } 12 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | // web-components-loader Copyright © 2016 Ray Nicholus 2 | const DOMParser = require('xmldom').DOMParser 3 | const fs = require('fs') 4 | const loaderUtils = require('loader-utils') 5 | const minifyHtml = require('html-minifier').minify 6 | const mkdirp = require('mkdirp') 7 | const path = require('path') 8 | const remotePathPattern = new RegExp('^(?:[a-z]+:)?//', 'i') 9 | const uglifyCss = require('uglifycss') 10 | const uglifyJs = require('uglify-js') 11 | 12 | module.exports = function(htmlFileContent) { 13 | const filesToEmit = 14 | [this.resourcePath].concat( 15 | getLocalDependencies(htmlFileContent, this.context) 16 | ) 17 | 18 | const emittedOutputPaths = [] 19 | 20 | const resourceDirName = this.context.split('/').pop() 21 | 22 | const htmlFileNameSansExt = this.resourcePath.split('/').pop().slice(0, -5) 23 | 24 | const parsedQuery = loaderUtils.parseQuery(this.query) 25 | 26 | const outputPath = parsedQuery.outputPath == null 27 | ? this.options.output.path 28 | : parsedQuery.outputPath 29 | 30 | const outputPublicPath = parsedQuery.outputPublicPath == null 31 | ? this.options.output.publicPath 32 | : parsedQuery.outputPublicPath 33 | 34 | const localOutputDir = `${outputPath}/web-components` 35 | 36 | const minify = parsedQuery.minify 37 | 38 | const outputDir = `${localOutputDir}/${resourceDirName}` 39 | 40 | this.cacheable() 41 | 42 | filesToEmit.forEach( 43 | filePath => { 44 | 'use strict' 45 | 46 | const relativePath = path.relative(this.context, filePath) 47 | const fileOutputPath = `${outputDir}/${relativePath}` 48 | const fileOutputDir = path.dirname(fileOutputPath) 49 | const rawContent = fs.readFileSync(filePath).toString() 50 | const transformJs = this.options.webComponentsLoader && this.options.webComponentsLoader.transformJs 51 | 52 | if (!fs.existsSync(fileOutputDir)) { 53 | mkdirp.sync(fileOutputDir) 54 | } 55 | 56 | let contentToOutput = rawContent 57 | 58 | if (transformJs && filePath.endsWith('.js')) { 59 | contentToOutput = transformJs(contentToOutput) 60 | } 61 | 62 | if (minify) { 63 | contentToOutput = getMinifiedOutput(filePath, contentToOutput) 64 | } 65 | 66 | fs.writeFileSync(fileOutputPath, contentToOutput) 67 | this.addDependency(filePath) 68 | emittedOutputPaths.push(fileOutputPath) 69 | } 70 | ) 71 | 72 | const htmlImportPath = `${outputPublicPath}web-components/${resourceDirName}/${htmlFileNameSansExt}.html` 73 | return `module.exports = '${htmlImportPath}'` 74 | } 75 | 76 | const getLocalDependencies = (htmlFileContent, htmlFilePath) => { 77 | const htmlFileDoc = new DOMParser().parseFromString(htmlFileContent, 'text/html') 78 | const localDependencies = [] 79 | 80 | Array.from(htmlFileDoc.getElementsByTagName('link')).forEach( 81 | linkEl => { 82 | const href = linkEl.getAttribute('href') 83 | 84 | // iff this is a local path 85 | if (href.trim() && !remotePathPattern.test(href)) { 86 | const relativePath = path.join(htmlFilePath, href) 87 | 88 | localDependencies.push(relativePath) 89 | 90 | // getLocalDependencies of this imported HTML file too 91 | if (linkEl.getAttribute('rel') === 'import') { 92 | const childHtmlFileContent = fs.readFileSync(relativePath).toString() 93 | const childHtmlFileDependencies = getLocalDependencies(childHtmlFileContent, path.dirname(relativePath)) 94 | 95 | localDependencies.push.apply(localDependencies, childHtmlFileDependencies) 96 | } 97 | } 98 | } 99 | ) 100 | 101 | Array.from(htmlFileDoc.getElementsByTagName('script')).forEach( 102 | scriptEl => { 103 | const src = scriptEl.getAttribute('src') 104 | 105 | // iff this is a local path 106 | if (src.trim() && !remotePathPattern.test(src)) { 107 | localDependencies.push(path.join(htmlFilePath, src)) 108 | } 109 | } 110 | ) 111 | 112 | return localDependencies 113 | } 114 | 115 | const getMinifiedHtml = unminifiedHtml => { 116 | return minifyHtml(unminifiedHtml, { 117 | collapseBooleanAttributes: true, 118 | collapseWhitespace: true, 119 | decodeEntities: true, 120 | minifyCSS: true, 121 | minifyJS: true, 122 | processConditionalComments: true, 123 | removeAttributeQuotes: true, 124 | removeComments: true, 125 | removeEmptyAttributes: true, 126 | removeOptionalTags: true, 127 | removeRedundantAttributes: true, 128 | removeScriptTypeAttributes: true, 129 | removeStyleLinkTypeAttributes: true, 130 | trimCustomFragments: true 131 | }) 132 | } 133 | 134 | const getMinifiedOutput = (filePath, code) => { 135 | if (filePath.endsWith('.html')) { 136 | return getMinifiedHtml(code) 137 | } 138 | else if (filePath.endsWith('.css')) { 139 | return uglifyCss.processString(code) 140 | } 141 | else if (filePath.endsWith('.js')) { 142 | return uglifyJs.minify(code, { fromString: true }).code 143 | } 144 | } --------------------------------------------------------------------------------