Skip to content

Commit

Permalink
Merge pull request #2 from Microsoft/ianc/loader-raw-script
Browse files Browse the repository at this point in the history
Ianc/loader raw script
  • Loading branch information
iclanton authored Feb 7, 2017
2 parents 74d4bcb + 8585006 commit b7ef22b
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ See individual projects for details:

[![npm version](https://badge.fury.io/js/%40microsoft%2Floader-load-themed-styles.svg)](https://badge.fury.io/js/%40microsoft%2Floader-load-themed-styles)
[![Dependencies](https://david-dm.org/Microsoft/loader-load-themed-styles.svg)](https://david-dm.org/Microsoft/loader-load-themed-styles)

### [@microsoft/loader-raw-script](./loader-raw-script/README.md)

`loader-raw-script` is a loader used for loading scripts with an `eval` statement.

[![npm version](https://badge.fury.io/js/%40microsoft%2Floader-raw-script.svg)](https://badge.fury.io/js/%40microsoft%2Floader-raw-script)
[![Dependencies](https://david-dm.org/Microsoft/loader-raw-script.svg)](https://david-dm.org/Microsoft/loader-raw-script)

10 changes: 10 additions & 0 deletions common/changes/ianc-loader-raw-script_2017-02-07-05-22.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/loader-raw-script",
"comment": "Setting up loader-raw-script.",
"type": "major"
}
],
"email": "[email protected]"
}
22 changes: 22 additions & 0 deletions common/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"rush-loader-load-themed-styles": "file:./temp_modules/rush-loader-load-themed-styles",
"rush-loader-raw-script": "file:./temp_modules/rush-loader-raw-script",
"rush-loader-set-webpack-public-path": "file:./temp_modules/rush-loader-set-webpack-public-path"
},
"description": "Temporary file generated by the Rush tool",
Expand Down
12 changes: 12 additions & 0 deletions common/temp_modules/rush-loader-raw-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "rush-loader-raw-script",
"version": "0.0.0",
"private": true,
"dependencies": {
"chai": "~3.5.0",
"gulp": "~3.9.1",
"mocha": "~3.2.0",
"@microsoft/node-library-build": "~2.2.0",
"loader-utils": "~0.2.16"
}
}
7 changes: 7 additions & 0 deletions loader-raw-script/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.DS_Store
npm*.log
coverage
temp
lib
dist
10 changes: 10 additions & 0 deletions loader-raw-script/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.vscode
src
node_modules

.gitignore
.gitattributes
.editorconfig
gulpfile.js
lib/**/*.test.js
lib/**/*.test.d.ts
24 changes: 24 additions & 0 deletions loader-raw-script/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@microsoft/loader-raw-script

Copyright (c) Microsoft Corporation. All rights reserved.

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 changes: 21 additions & 0 deletions loader-raw-script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# raw-script loader for webpack

## Installation

`npm @microsoft/loader-raw-script --save-dev`

## Overview

This simple loader loads a script file's contents directly in a webpack bundle using an `eval(...)`.

## Usage

[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)

``` javascript
require("@microsoft/loader-raw-script!path/to/script.js");
```

## License

MIT (http://www.opensource.org/licenses/mit-license.php)
5 changes: 5 additions & 0 deletions loader-raw-script/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

let build = require('@microsoft/node-library-build');

build.initialize(require('gulp'));
23 changes: 23 additions & 0 deletions loader-raw-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@microsoft/loader-raw-script",
"version": "0.0.5",
"description": "",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"license": "MIT",
"repository": "github:microsoft/webpack-loaders",
"scripts": {
"build": "gulp",
"clean": "gulp clean",
"test": "gulp test"
},
"dependencies": {
"loader-utils": "~0.2.16"
},
"devDependencies": {
"chai": "~3.5.0",
"gulp": "~3.9.1",
"mocha": "~3.2.0",
"@microsoft/node-library-build": "~2.2.0"
}
}
32 changes: 32 additions & 0 deletions loader-raw-script/src/RawScriptLoader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/

import { assert } from 'chai';
import RawScriptLoader = require('./RawScriptLoader');

function wrapResult(result: string): string {
return `var exports = {};
eval(${result});
exports;`;
}

describe('RawScriptLoader', () => {
it('follows the Webpack loader interface', () => {
assert.isDefined(RawScriptLoader);
assert.isFunction(RawScriptLoader);
});

it('returns a string', () => {
assert.isString(RawScriptLoader(''));
});

it('correctly sets exported objects', () => {
const testScript: string = 'var x = 123; this.exportedObject = x;';
/* tslint:disable:no-eval */
const exports: { exportedObject: number } = eval(wrapResult(RawScriptLoader(testScript)));
/* tslint:enable:no-eval */
assert.equal(exports.exportedObject, 123);
});
});
25 changes: 25 additions & 0 deletions loader-raw-script/src/RawScriptLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/

import { EOL } from 'os';

const loaderFn: (content: string) => string = (content: string) => {
content = content.replace(/\\/g, '\\\\');
content = content.replace(/'/g, '\\\'');
content = content.replace(/\n/g, '\\n');
content = content.replace(/\r/g, '\\r');

const lines: string[] = [
'(function (global) {',
` eval('${content}');`,
'}.call(exports, (function() { return this; }())))'
];

return lines.join(EOL);
};

/* tslint:disable:export-name */
export = loaderFn;
/* tslint:enable:export-name */
10 changes: 10 additions & 0 deletions loader-raw-script/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/

import * as RawScriptLoader from './RawScriptLoader';

/* tslint:disable:export-name */
export = RawScriptLoader;
/* tslint:enable:export-name */
10 changes: 10 additions & 0 deletions loader-raw-script/src/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/

var context = require.context('.', true, /.+\.test\.js?$/);

context.keys().forEach(context);

module.exports = context;
9 changes: 9 additions & 0 deletions loader-raw-script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"jsx": "react",
"declaration": true,
"sourceMap": true
}
}
6 changes: 6 additions & 0 deletions rush.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"projectFolder": "loader-set-webpack-public-path",
"reviewCategory": "libraries",
"shouldPublish": true
},
{
"packageName": "@microsoft/loader-raw-script",
"projectFolder": "loader-raw-script",
"reviewCategory": "libraries",
"shouldPublish": true
}
]
}

0 comments on commit b7ef22b

Please sign in to comment.